package com.bilin.coin.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bilin.coin.R;
import com.bilin.coin.bean.CommonSuccessEvent;
import com.bilin.coin.bean.SellGoldParamBean;
import com.bilin.coin.common.base.BaseAppCompatActivity;
import com.bilin.coin.tools.Geter;
import com.bilin.coin.tools.Poster;
import com.bilin.coin.utils.GsonUtil;
import com.bilin.coin.utils.MoneyUtils;

import org.greenrobot.eventbus.EventBus;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = "/auction/SellGoldActivity")
public class SellGoldActivity extends BaseAppCompatActivity {

    @BindView(R.id.et_sell_num)
    EditText et_sell_num;
    @BindView(R.id.et_account)
    EditText et_account;
    @BindView(R.id.et_pass)
    EditText et_pass;

    @BindView(R.id.tv_usable_gold)
    TextView tv_usable_gold;
    @BindView(R.id.tv_check)
    TextView tv_check;
    @BindView(R.id.tv_dis_gold)
    TextView tv_dis_gold;
    @BindView(R.id.tv_dis_seed)
    TextView tv_dis_seed;
    @BindView(R.id.tv_suc_gold)
    TextView tv_suc_gold;

    @BindView(R.id.ll_check_success)
    LinearLayout ll_check_success;
    private SellGoldParamBean sellGoldParamBean;


    @Override
    protected void initView() {
        setTitle("挂卖金豆");
        setMenu("挂卖金豆记录");
        initEvent();
        getDataParam();
    }

    private void initEvent() {
        et_sell_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String trim = et_sell_num.getText().toString().trim();
                try {
                    double gold = Double.parseDouble(trim) * Double.parseDouble(sellGoldParamBean.getJdRate());
                    double seed = Double.parseDouble(trim) * Double.parseDouble(sellGoldParamBean.getZzRate());
                    tv_dis_gold.setText(MoneyUtils.decimal2ByUp2(new BigDecimal(gold)));
                    tv_dis_seed.setText(MoneyUtils.decimal2ByUp2(new BigDecimal(seed)));
                    tv_suc_gold.setText(MoneyUtils.decimal2ByUp2(new BigDecimal(trim).subtract(new BigDecimal(gold))));
                } catch (Exception e) {
                    tv_dis_gold.setText("0");
                    tv_dis_seed.setText("0");
                    //et_sell_num.setText("0");
                    tv_suc_gold.setText("0");
                }
            }
        });
    }

    private void getDataParam() {
        new Geter(this, false, true) {
            @Override
            protected String fillUrl() {
                return "/api/bl-sell-jd-log/getParams";
            }

            @Override
            protected Map<String, String> fillParams() {
                return new HashMap<>();
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                sellGoldParamBean = GsonUtil.GsonToBean(dataJson, SellGoldParamBean.class);
                fillData();
            }
        };
    }

    private void fillData() {
        et_sell_num.setHint("最小挂卖数量"+sellGoldParamBean.getMin());
        tv_usable_gold.setText("可用金豆: "+sellGoldParamBean.getUsable());
    }

    @Override
    public void setMenuOnClickListener() {
        super.setMenuOnClickListener();
        baseStartActivity("/auction/GuaSellGoldRecordActivity");
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tv_check, R.id.bt_confirm,R.id.tv_all})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_check:
                check();
                break;
            case R.id.bt_confirm:
                confirm();
                break;
            case R.id.tv_all:
                if (sellGoldParamBean != null) {
                    et_sell_num.setText(sellGoldParamBean.getUsable());
                }
                break;
        }
    }

    private void confirm() {
        String pass = et_pass.getText().toString().trim();
        //String account = et_account.getText().toString().trim();
        String amount = et_sell_num.getText().toString().trim();
        if (TextUtils.isEmpty(pass)){
            showToast("密码不能为空");
            return;
        }
       /* if (TextUtils.isEmpty(account)){
            showToast("账号不能为空");
            return;
        }*/
        if (TextUtils.isEmpty(amount)){
            showToast("数量不能为空");
            return;
        }
        new Poster(this, true, true) {
            @Override
            protected String fillUrl() {
                return "/api/bl-sell-jd-log/sellJd";
            }

            @Override
            protected Map<String, String> fillParams() {
                HashMap hashMap = new HashMap();
                hashMap.put("safeWord", pass);
                hashMap.put("amount", amount);
                //hashMap.put("account", account);
                return hashMap;
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                EventBus.getDefault().post(new CommonSuccessEvent(CommonSuccessEvent.SellGoldSuccess));
                finish();
            }
        };
    }

    private void check() {
        String account = et_account.getText().toString().trim();
        new Geter(this, false, true) {
            @Override
            protected String fillUrl() {
                return "/api/bl-sell-jd-log/sellJdAccountCheck";
            }

            @Override
            protected Map<String, String> fillParams() {
                HashMap hashMap = new HashMap();
                hashMap.put("account",account);
                return hashMap;
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                if ("true".equals(dataJson)) {
                    ll_check_success.setVisibility(View.VISIBLE);
                } else {
                    showToast("该账号不存在");
                    ll_check_success.setVisibility(View.GONE);
                }
            }
        };
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sell_gold;
    }
}
